<template>
  <div :class="{ hidden: hidden }" class="pagination-container">
    <el-pagination
      :small="small"
      :background="background"
      :page-size.sync="pageSize"
      :total="total"
      :current-page.sync="currentPage"
      :layout="layout"
      :page-sizes="pageSizes"
      :prev-text="preText"
      :next-text="nextText"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
  </div>
</template>

<script>
import { scrollTo } from '@/utils/scroll-to'

export default {
  name: 'Pagination',
  props: {
    small: {
      type: Boolean,
      default: false,
    },
    background: {
      type: Boolean,
      default: true,
    },
    total: {
      required: true,
      type: Number,
    },
    current: {
      type: Number,
      default: 1,
    },
    size: {
      type: Number,
      default: 20,
    },
    pageSizes: {
      type: Array,
      default() {
        return [10, 20, 30, 50]
      },
    },
    preText: {
      type: String,
      default: '上一页',
    },
    nextText: {
      type: String,
      default: '下一页',
    },
    layout: {
      type: String,
      default: 'total, prev, pager, next, jumper',
    },
    hidden: {
      type: Boolean,
      default: false,
    },
    autoScroll: {
      type: Boolean,
      default: true,
    },
  },
  computed: {
    pageSize: {
      get() {
        return this.size
      },
      set(val) {
        this.$emit('update:size', val)
      },
    },
    currentPage: {
      get() {
        return this.current
      },
      set(val) {
        this.$emit('update:current', val)
      },
    },
  },
  methods: {
    handleSizeChange(val) {
      this.$emit('pagination', { current: this.currentPage, size: val })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    },
    handleCurrentChange(val) {
      this.$emit('pagination', { current: val, size: this.pageSize })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.pagination-container {
  width: 100%;
  text-align: center;

  margin-bottom: 10px;
  margin-top: 10px;
  padding-top: 10px;
  padding-bottom: 10px;

  // margin-top: 30px;

  // position: absolute;
  // bottom: 0;
}

.pagination-container.hidden {
  display: none;
}
</style>
